package hibernate.web;

import hibernate.fileControler.MyException;
import hibernate.model.Sirketprofil;
import hibernate.service.EmmyService;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@SuppressWarnings("serial")
@Component("SirketprofilBean")
@Scope("session")
public class SirketprofilBean implements BeanService {

	@Autowired
	private transient EmmyService emmyService;
	
	private Sirketprofil currentSirketpro;	
	private List<Sirketprofil> sirketproList;
	private Sirketprofil selectedSirket;	
	
	@SuppressWarnings("unchecked")
	@Override
	@PostConstruct
	public void init(){		
		sirketproList = (List<Sirketprofil>) emmyService.findAll(new Sirketprofil());
	}

	@Override
	public String restart() {
		init();
		return "list";
	}

	@Override
	public String deleteView() {
		emmyService.delete(currentSirketpro.getFirmaid());
		init();
		return "list";
	}

	@Override
	public String viewNew() {
		currentSirketpro= new Sirketprofil();
		return "detail";
	}

	@Override
	public String viewDetail() {/*
		List<Sirketprofil> valueList = (List<Sirketprofil>) sirketproTable.getValue();	
		System.out.println("table size : "+valueList.size());		
		Iterator<SimpleRowKey> keys = sirketproTable.getSelection().getKeys();
		
		if(keys.hasNext()){
			currentSirketpro= valueList.get(keys.next().intValue());
			System.out.println("dolu = "+currentSirketpro.getFirmaid());
			return "detail";
		}else{
			System.out.println("bo�ta");
			return null;
		}*/ return"detay";
	}

	@Override
	public String SaveOrUpdate() throws MyException{
		FacesMessage msg = null; 
		try{
			emmyService.saveOrUpdate(currentSirketpro);
			msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", "Process Completed...");  
	        FacesContext.getCurrentInstance().addMessage(null, msg);
		}catch(Exception e){
			System.out.println("hata : "+e);
			msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "unsuccessful", "İşlem Başarısız, Tekrar deneyiniz.");  
	        FacesContext.getCurrentInstance().addMessage(null, msg);
		}
		
		//init();
		return "";
	}
	
	@SuppressWarnings("unchecked")
	public void getSirketList(){
		sirketproList = (List<Sirketprofil>) emmyService.findAll(new Sirketprofil());
	}
	
	public void deleteSelectedSirket(){
		selectedSirket =null;
	}
	
	public void deleteCurrentSirket(){ //deleteParamToContext
		//httpSession.removeAttribute("currentSirketprofil");
		currentSirketpro =null;
		getSirketList();
	}
	
	@Override
	public void getSession(){
		
	}

	@SuppressWarnings("unchecked")
	public String getDetayliSirketPro(){		
		sirketproList = (List<Sirketprofil>) emmyService.getQuery(createQuery());
		return "list";
	}
	
	@Override
	public String createQuery() {
		Sirketprofil srp  = currentSirketpro;		
		String query = "from Sirketprofil";				
		
		if(srp == null){
			return query;
		}		
		if(srp.getFirmaAd() != null){
			if(query.indexOf("where") != -1){
				query += " and ad firmaAd '%"+srp.getFirmaAd()+"%'";
				
			}
			else{
				query += " where ad firmaAd '%"+srp.getFirmaAd()+"%'";			
			}
		}
		if(srp.getTel1() != 0){
			if(query.indexOf("where") != -1){
				query += " and tel1="+srp.getTel1()+"";
				
			}
			else{
				query += " where tel1="+srp.getTel1()+"";			
			}
		}
		if(srp.getTel2() != 0){
			if(query.indexOf("where") != -1){
				query += " and tel2="+srp.getTel2()+"";
				
			}
			else{
				query += " where tel2="+srp.getTel2()+"";			
			}
		}
		if(srp.getFax() != 0){
			if(query.indexOf("where") != -1){
				query += " and fax="+srp.getFax()+"";
				
			}
			else{
				query += " where fax="+srp.getFax()+"";			
			}
		}
		if(srp.getEmail() != null){
			if(query.indexOf("where") != -1){
				query += " and email like '%"+srp.getEmail()+"%'";
				
			}
			else{
				query += " where email like '%"+srp.getIl()+"%'";			
			}
		}
		if(srp.getIl() != null){
			if(query.indexOf("where") != -1){
				query += " and il like '%"+srp.getIl()+"%'";
				
			}
			else{
				query += " where il like '%"+srp.getIl()+"%'";			
			}
		}
		if(srp.getIlce() != null){
			if(query.indexOf("where") != -1){
				query += " and ilce like '%"+srp.getIlce()+"%'";
				
			}
			else{
				query += " where ilce like '%"+srp.getIlce()+"%'";			
			}
		}
		if(srp.getMahalle() != null){
			if(query.indexOf("where") != -1){
				query += " and mahalle like '%"+srp.getMahalle()+"%'";
				
			}
			else{
				query += " where mahalle like '%"+srp.getMahalle()+"%'";			
			}
		}
		if(srp.getWebAdd() != null){
			if(query.indexOf("where") != -1){
				query += " and webAdd like '%"+srp.getWebAdd()+"%'";
				
			}
			else{
				query += " where webAdd like '%"+srp.getWebAdd()+"%'";			
			}
		}
		return query;
	}

	public Sirketprofil getCurrentSirketpro() {
		return currentSirketpro;
	}

	public void setCurrentSirketpro(Sirketprofil currentSirketpro) {
		this.currentSirketpro = currentSirketpro;
	}

	public List<Sirketprofil> getSirketproList() {
		return sirketproList;
	}

	public void setSirketproList(List<Sirketprofil> sirketproList) {
		this.sirketproList = sirketproList;
	}
	
	public Sirketprofil getSelectedSirket() {
		return selectedSirket;
	}

	public void setSelectedSirket(Sirketprofil selectedSirket) {
		this.selectedSirket = selectedSirket;
	}
}